﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using utils;

namespace dal.cgm_production_dao
{
    public class PrintColorBoxInfo
    {
        [DataMember]
        public int id { get; set; }
        [DataMember]
        public string production_code { get; set; }
        [DataMember]
        public string production_batch { get; set; }
        [DataMember]
        public string production_date { get; set; }
        [DataMember]
        public string expiry_date { get; set; }
        [DataMember]
        public string mac_address { get; set; }
        [DataMember]
        public string serial_code { get; set; }
        [DataMember]
        public string operator_code { get; set; }
        [DataMember]
        public DateTime create_time { get; set; }
        [DataMember]
        public DateTime update_time { get; set; }
        [DataMember]
        public int print_count { get; set; }
    }

    public class PrintColorBoxInfoDao
    {
        public static void Insert(PrintColorBoxInfo data)
        {
            string sql = @"INSERT INTO print_colorbox_info
           (production_code,production_batch,production_date,expiry_date,mac_address,serial_code,operator_code,create_time,update_time,print_count)
         VALUES
            (@production_code,@production_batch,@production_date,@expiry_date,@mac_address,@serial_code,@operator_code,@create_time,@update_time,@print_count)";
            List<MySqlParameter> parameters = new List<MySqlParameter>();
            parameters.Add(new MySqlParameter("@production_code", data.production_code));
            parameters.Add(new MySqlParameter("@production_batch", data.production_batch));
            parameters.Add(new MySqlParameter("@production_date", data.production_date));
            parameters.Add(new MySqlParameter("@expiry_date", data.expiry_date));
            parameters.Add(new MySqlParameter("@mac_address", data.mac_address));
            parameters.Add(new MySqlParameter("@serial_code", data.serial_code));
            parameters.Add(new MySqlParameter("@operator_code", data.operator_code));
            parameters.Add(new MySqlParameter("@create_time", data.create_time));
            parameters.Add(new MySqlParameter("@update_time", data.update_time));
            parameters.Add(new MySqlParameter("@print_count", data.print_count));
            MySqlHelper.ExecuteNonQuery(ConfigString.cgm_production, sql, parameters.ToArray());
        }

        public static PrintColorBoxInfo GetMac(string mac)
        {
            string sql = @"SELECT id,production_code,production_batch,production_date,expiry_date,mac_address,serial_code,operator_code,create_time,update_time,print_count FROM print_colorbox_info WHERE mac_address=@mac_address";
            List<MySqlParameter> parameters = new List<MySqlParameter>();
            parameters.Add(new MySqlParameter("@mac_address", mac));
            DataSet ds = MySqlHelper.ExecuteDataset(ConfigString.cgm_production, sql, parameters.ToArray());

            IList<PrintColorBoxInfo> list = DataContainerUtil.DataTableToList<PrintColorBoxInfo>(ds.Tables[0]);
            if (list.Count > 0)
            {
                return list[0];
            }
            else
            {
                return new PrintColorBoxInfo();
            }
        }

        public static PrintColorBoxInfo GetSerialCode(string serial_code)
        {
            string sql = @"SELECT id,production_code,production_batch,production_date,expiry_date,mac_address,serial_code,operator_code,create_time,update_time,print_count FROM print_colorbox_info WHERE serial_code=@serial_code";
            List<MySqlParameter> parameters = new List<MySqlParameter>();
            parameters.Add(new MySqlParameter("@serial_code", serial_code));
            DataSet ds = MySqlHelper.ExecuteDataset(ConfigString.cgm_production, sql, parameters.ToArray());

            IList<PrintColorBoxInfo> list = DataContainerUtil.DataTableToList<PrintColorBoxInfo>(ds.Tables[0]);
            if (list.Count > 0)
            {
                return list[0];
            }
            else
            {
                return new PrintColorBoxInfo();
            }
        }



        public static void UpDate(PrintColorBoxInfo info)
        {
            if (info != null)
            {
                List<MySqlParameter> parameters = new List<MySqlParameter>();
                string sql = " UPDATE print_colorbox_info SET";
                sql = sql + " count=@count,";
                parameters.Add(new MySqlParameter("@print_count", info.print_count));
                sql = sql + " update_time=@update_time";
                parameters.Add(new MySqlParameter("@update_time", info.update_time));
                sql = sql + " WHERE production_batch=@production_batch";
                parameters.Add(new MySqlParameter("@production_batch", info.production_batch));
                sql = sql + " and  mac_address=@mac_address";
                parameters.Add(new MySqlParameter("@mac_address", info.mac_address));

                MySqlHelper.ExecuteDataset(ConfigString.cgm_production, sql, parameters.ToArray());
            }
        }
    }
}
